package ru.agiledev.aos.frontend.server;

import ru.agiledev.aos.commons.dto.SemanticQuery;

import java.util.HashMap;

/**
 * Created by a.kirillov
 * Date: 2/24/12 Time: 5:52 PM
 */
public class QueryRegistry {
    private QueryRegistry() {
        registry = new HashMap<String, SemanticQuery>();
    }

    private HashMap<String, SemanticQuery> registry;


    private static class SingletonHolder {
        public static final QueryRegistry instance = new QueryRegistry();
    }

    public static QueryRegistry getInstance() {
        return SingletonHolder.instance;
    }

    public void registerQuery(SemanticQuery query, String key){
        synchronized (registry){
            if(!registry.containsKey(key)){
                registry.put(key, query);
            }
        }
    }

    public boolean contains(String key){
        return registry.containsKey(key);
    }

    public SemanticQuery getQuery(String key){
        if(registry.containsKey(key)){
            SemanticQuery result = registry.get(key);
            registry.remove(key);

            return result;
        } else {
            return null;
        }
    }

}
